layui.use(['layer', 'element', 'laytpl', 'jquery', 'table'], function() {
	//引入模块
	var layer = layui.layer,
		element = layui.element,
		laytpl = layui.laytpl,
		table = layui.table,
		$ = layui.jquery;

	if (localStorage.set != null) {
		//加载页面发生请求拿到数据
		$.ajax({
			url: "http://localhost:8080/sc/admin?type=home",
			async: true, //异步
			type: "POST",
			headers: {
				"token": localStorage.token
			},
			success: function(data) {
				//这里找id必须用js
				if (data.code == 'TokenFailed') {
					//删除token set
					delete localStorage.token;
					delete localStorage.set;
					layer.msg('登录失效，请重新登录！')
					setTimeout(function() {
						//调用父页面方法
						parent.tz();
					}, 1000);
				} else {
					var getTpl = document.getElementById('homemb').innerHTML,
						view = document.getElementById('homemoban');
					laytpl(getTpl).render(data, function(html) {
						view.innerHTML = html;
					});
				}
			}
		});

	} else {
		layer.msg('您还没有登录！')
		setTimeout(function() {
			parent.tz();
			//window.location.replace('admin_login.html');
		}, 1000);
	}

	//自动重载
	var token = localStorage.token;
	//表格重载
	table.reload('testReload', {
		url: 'http://localhost:8080/sc/admin?type=users',
		headers: {
			token: token
		} //设定异步数据接口的额外参数
	});

	//载入请求用户登录状态有效
	$.ajax({
		url: "http://localhost:8080/sc/admin?type=login",
		async: true, //异步
		type: "POST",
		headers: {
			"token": localStorage.token
		},
		success: function(data) {
			//这里找id必须用js
			if (data.code == 'TokenFailed') {
				//删除token set
				delete localStorage.token;
				delete localStorage.set;
				layer.msg('登录失效，请重新登录！')
				setTimeout(function() {
					//调用父页面方法
					parent.tz();
				}, 1000);
			}
		}
	})

	//监听行工具栏
	table.on('tool(tooltable)', function(obj) {
		var data = obj.data; //获得当前行数据
		var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
		var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
		if (layEvent == 'del') { //等于活动名封号
			if (data.authority == '2') {
				layer.alert('已经被封禁，不能在点咯', {
					icon: 1,
					skin: 'layer-ext-demo' //见：扩展说明
				})
			} else {
				layer.confirm('您确定要封禁这位用户的账号吗？', {
					btn: ['是的', '取消'] //按钮
				}, function() {
					$.ajax({
						url: "http://localhost:8080/sc/admin2?type=Userqx&zt=2&id=" +
							data.id,
						async: true, //异步
						type: "POST",
						headers: {
							"token": localStorage.token
						},
						success: function(data) {
							layer.alert('成功封禁', {
								icon: 1,
								skin: 'layer-ext-demo' //见：扩展说明
							})
						}
					});
					//更新数据
					obj.update({
						authority: '2',
					});
				}, function() {
					layer.msg('成功取消', {
						time: 5000, //20s后自动关闭
						btn: ['知道了']
					});
				});
			}
		} else if (layEvent == 'unblock') { //解封
			if (data.authority == '1') {
				layer.alert('账号状态正常，不能在点咯', {
					icon: 1,
					skin: 'layer-ext-demo' //见：扩展说明
				})
			} else {
				layer.confirm('您确定要给这位用户解封吗？', {
					btn: ['确定', '取消'] //按钮
				}, function() {
					$.ajax({
						url: "http://localhost:8080/sc/admin2?type=Userqx&zt=1&id=" +
							data.id,
						async: true, //异步
						type: "POST",
						headers: {
							"token": localStorage.token
						},
						success: function(data) {
							layer.alert('成功解封', {
								icon: 1,
								skin: 'layer-ext-demo' //见：扩展说明
							})
						}
					});
					//更新数据
					obj.update({
						authority: '1',
					});
				}, function() {
					layer.msg('成功取消', {
						time: 5000, //20s后自动关闭
						btn: ['知道了']
					});
				});
			}

		} else if (layEvent == 'detail') { //查看

			$.ajax({
				url: "http://localhost:8080/sc/admin2?type=getAddress&id=" + data.id,
				async: true, //异步
				type: "POST",
				headers: {
					"token": localStorage.token
				},
				success: function(data) {
					$('#look_address').text(data.address);
				}
			});
			//给窗口赋值
			$("#look_img").attr("src", "http://localhost:8080/sc/images/" + data.portrait);
			$('#look_username').text(data.username);
			$('#look_name').text(data.name);
			$('#look_sex').text(data.sex);
			$('#look_email').text(data.mail);
			$('#look_phone').text(data.tel);
			$('#look_brithdate').text(data.birthdate);
			$('#look_auth').text(data.authority);
			switch (data.authority) {
				case 1:
					$('#look_auth').text('正常状态');
					break;
				case 2:
					$('#look_auth').text('封号状态');
					break;
			}

			layer.open({
				type: 1,
				area: ['600px', '500px'],
				title: '查看用户信息',
				content: $("#look_user_window"),
				shade: 0,
				btn: ['确定'],
				btn1: function(index, layero) {
					layer.closeAll();
				},
				cancel: function(layero, index) {
					layer.closeAll();
				}
			});
		}
	})

	//头工具栏事件
	table.on('toolbar(tooltable)', function(obj) {
		var checkStatus = table.checkStatus(obj.config.id); //获得表格选中的数据
		switch (obj.event) {
			case 'alldel': //批量封号
				var data = checkStatus.data;
				if (checkStatus.data.length != 0) {
					for (let var1 in checkStatus.data) { //解析json多个对象
						if (data[var1].authority == '2') {
							layer.msg('请检查是否有选中已经被封禁的用户', {
								time: 5000, //20s后自动关闭
								btn: ['知道了']
							});
							return;
						}
					}

					layer.confirm('您确定要批量封禁这些用户吗？', {
						btn: ['确定', '取消'] //按钮
					}, function() {
						//加载提示框
						var index = layer.load(1, {
							shade: [0.5, '#fff'] //0.1透明度的白色背景
						});
						var data = checkStatus.data;
						var id = '';
						for (let var1 in data) { //解析json多个对象
							id = id + '&id=' + (data[var1].id);
						}
						//请求下架
						$.ajax({
							url: "http://localhost:8080/sc/admin2?type=Userqx&zt=2" +
								id,
							async: true, //异步
							type: "POST",
							headers: {
								"token": localStorage.token
							},
							success: function(data) {
								//关闭加载窗
								layer.close(index);
								layer.msg('成功封禁', {
									time: 5000, //20s后自动关闭
									btn: ['知道了']
								});
								//表格重载
								table.reload('testReload', {
									url: 'http://localhost:8080/sc/admin?type=users',
									headers: {
										token: token
									} //设定异步数据接口的额外参数
								});
							}
						});
					}, function() {
						layer.msg('成功取消', {
							time: 5000, //20s后自动关闭
							btn: ['知道了']
						});
					});
				} else {
					layer.msg('您还没选要封号的用户');
				}


				break;
		};
	});

	//搜索点击事件
	$('#ss').on('click', function() {
		//拿到输入框的值
		var keyword = $('#ssinput').val();
		var token = localStorage.token;
		//表格重载
		table.reload('testReload', {
			url: 'http://localhost:8080/sc/admin?type=ssusers',
			where: {
				keyword: keyword
			},
			headers: {
				token: token
			} //设定异步数据接口的额外参数
		});
	});
});
